package com.hp.gxw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.gxw.constant.LayTableR;
import com.hp.gxw.entity.Customer;
import com.hp.gxw.service.ICustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/manage/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @PostMapping("/list")
    @ResponseBody
    public LayTableR list(Integer page, Integer limit, String key) {
        QueryWrapper<Customer> ew = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(key)) {
            ew.like("code", key);
            ew.or();
            ew.like("name", key);
        }
        ew.orderByDesc("code");
        Page<Customer> customerPage = customerService.page(new Page<>(page, limit), ew);
        return LayTableR.result(0, customerPage.getTotal(), customerPage.getRecords());
    }

}
